package com.apartmentManage.controller;

import com.apartmentManage.entity.Room;
import com.apartmentManage.service.RoomService;
import com.apartmentManage.until.Renter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @PostMapping("/save")
    String saveList(Integer height, Integer perNumber,
                    Integer headNo, Integer intoNumber, Integer apartmentNo){
        boolean flag =
                roomService.saveList(height, perNumber, headNo, intoNumber, apartmentNo);
        if (flag)
            return "redirect:/room/getByApartmentNo/"+apartmentNo;
        return "redirect:/room/addView/"+apartmentNo;
    }

    @GetMapping("/addView/{apartmentNo}")
    public String addView(@PathVariable("apartmentNo") Integer apartmentNo, Model model){
        model.addAttribute("apartmentNo",apartmentNo);
        return "pages/room/add";
    }

    /*查询*/
//    @GetMapping("/getAll")
//    String getAll(Model model){
//        List<Room> result = roomService.getAll();
//        new Renter(result==null?false:true,result);
//        model.addAttribute("result",result);
//        return "pages/room/List";
//    }

    /*根据apartmentNo查询*/
    @GetMapping("/getByApartmentNo/{apartmentNo}")
    String getByApartmentNo(@PathVariable("apartmentNo") Integer apartmentNo,Model model){
        List<Room> result = roomService.getByApartmentNo(apartmentNo);
        new Renter(result==null?false:true,result);
        model.addAttribute("result",result);
        return "pages/room/List";
    }
}
